package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HoStudentInfoMapper;
import com.ruoyi.system.domain.HoStudentInfo;
import com.ruoyi.system.service.IHoStudentInfoService;

/**
 * 学生信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class HoStudentInfoServiceImpl implements IHoStudentInfoService {
    @Autowired
    private HoStudentInfoMapper hoStudentInfoMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public HoStudentInfo selectHoStudentInfoById(Long id) {
        return hoStudentInfoMapper.selectHoStudentInfoById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param hoStudentInfo 学生信息
     * @return 学生信息
     */
    @Override
    public List<HoStudentInfo> selectHoStudentInfoList(HoStudentInfo hoStudentInfo) {
        return hoStudentInfoMapper.selectHoStudentInfoList(hoStudentInfo);
    }

    /**
     * 新增学生信息
     *
     * @param hoStudentInfo 学生信息
     * @return 结果
     */
    @Override
    public int insertHoStudentInfo(HoStudentInfo hoStudentInfo) {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(101L);
        sysUser.setUserName(hoStudentInfo.getStudentNum());
        sysUser.setNickName(hoStudentInfo.getName());
        sysUser.setPhonenumber(hoStudentInfo.getPhone());
        sysUser.setSex(hoStudentInfo.getGender());
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUserService.addStudentUser(sysUser);
        hoStudentInfo.setCreateTime(DateUtils.getNowDate());
        return hoStudentInfoMapper.insertHoStudentInfo(hoStudentInfo);
    }

    /**
     * 修改学生信息
     *
     * @param hoStudentInfo 学生信息
     * @return 结果
     */
    @Override
    public int updateHoStudentInfo(HoStudentInfo hoStudentInfo) {
        hoStudentInfo.setUpdateTime(DateUtils.getNowDate());
        return hoStudentInfoMapper.updateHoStudentInfo(hoStudentInfo);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteHoStudentInfoByIds(Long[] ids) {
        for (Long id : ids) {
            HoStudentInfo hoStudentInfo = hoStudentInfoMapper.selectHoStudentInfoById(id);
            SysUser sysUser = sysUserService.selectUserByUserName(hoStudentInfo.getStudentNum());
            if (null != sysUser) {
                sysUserService.deleteUserById(sysUser.getUserId());
            }
        }
        return hoStudentInfoMapper.deleteHoStudentInfoByIds(ids);
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteHoStudentInfoById(Long id) {
        return hoStudentInfoMapper.deleteHoStudentInfoById(id);
    }
}
